<?php
/**
 * 医生信息管理控制器
 * Created by PhpStorm.
 * User: change
 * Date: 2016/12/22
 * Time: 22:17
 */

namespace Admin\Controller;
use Admin\Model\AerainfoModel;
use Base\Controller\CurdController;
use Library\BmobSms;
use Library\UploadFile;
class DoctorInfoController extends CurdController
{
        public function __construct()
    {
        parent::__construct(true, true, array(
            'input_name' => 'coverFile',//文本框name
            'save_path' => 'doctorcover',//保存路径
            'data_field' => 'cover'//数据库字段
        ));
     }

    protected function getAll()
    {
        if(!I('get.del')){
            $where['status']=array('egt',0);
        }else{
            $where['status']=-1;
        }
        if(I('get.reviewed')==-3){
            $where['status']=0;
        }
        if(I('get.reviewed')==1){
           $where['status']=I('get.reviewed');
        }
        if(I('get.reviewed')==2){
           $where['status']=I('get.reviewed');
        }
        if($_REQUEST['keyword']){
            $map['phone'] = array('like','%'.$_REQUEST['keyword'].'%');
            $map['realname']  = array('like', '%'.$_REQUEST['keyword'].'%');
            $map['_logic'] = 'or';
            $where['_complex'] = $map;
        }
        if($_REQUEST['departmentid']){
            $where['officeid']=$_REQUEST['departmentid'];
        }
       if($_REQUEST['hospitalid']){
           $where['hospitalid']=$_REQUEST['hospitalid'];
       }
        if($_REQUEST['selects']){
            $limit=$_REQUEST['selects'];
        }else{
            $limit=20;
        }
//        dump($where);
        $data = $this->model->pageToView($limit, true,$where);
//        dump($data['list']);
        $this->assign("page", $data['page']);
        $this->assign("list", $data['list']);
    }
    /**
     * @param string $province_id
     * 根据省市获取市区的数据
     */
    public function getCityListJson($province_id = '')
    {
        $AerainfoModel= new AerainfoModel();
        $list = $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where(array('PARENT_ID'=>$province_id))->select();
        $this->ajaxReturn($list);
    }
    /**
     * @param string $city_id
     * 根据市区获取县区的数据
     */
    public function getDistrictJson($city_id=""){
        $AerainfoModel= new AerainfoModel();
        $list= $AerainfoModel->relation(false)->field("aerainfo_id,aerainfo_name")->where(array('PARENT_ID'=>$city_id))->select();
        $this->ajaxReturn($list);
    }


    /**
     * 新增医生信息
     */
    public function add()
    {
        $data = $this->model->create();
        if (empty($data)) {
            $this->error($this->model->getError());
        } else {
            //检查密码
            $this->checkPwd();
            //上传医生图片
            $data['passwd']=md5(C('md5s').$data['passwd']);
            $englishname=$this->piny($data['realname']);
            $data['en_realname']=$englishname;
            $data = $this->upDoctorImg($data,false);
            $data = $this->addAfter($data);
            $add = $this->model->add($data);
            $datas['id']=$add;
            $datas['name']=C('Doctor').$add;
            $res=$this->model->save($datas);
            $this->addEnd($res);
        }
    }

    /**
     * 编辑医生信息
     */
    public function edit()
    {
        //检查密码
        $this->checkPwd();
        $data = $this->model->create();
        if (empty($data)) {
            $this->error($this->model->getError());
        } else {
            //检查密码
            $this->checkPwd();
            if ($data['passwd'] == '') unset($data['pwd']);
            if ($data['certCover'] == '') unset($data['certCover']);
			if($data['bloodid']=="")unset($data['bloodid']);
            //上传医生图片
           if(empty($data['birthday'])){
               $data['birthday']=date("Y-m-d H:i:s", time());
            }
            $data = $this->upDoctorImg($data,true);
            $result = $this->model->save($data);
            if ($result) {
                $this->success('操作成功');
            }
        }
    }

    /**
     * @param $data
     * @param $isEdit
     * @return mixed
     */
    function upDoctorImg($data,$isEdit){
        if ($this->upload and $_FILES) {
            $input_name = $this->uploadConfig['input_name'];
            $save_path = $this->uploadConfig['save_path'];
            $data_field = $this->uploadConfig['data_field'];
            $up = new UploadFile();
            $info = $up->upDifferentImg('cover');
//                $this->error(json_encode($data[$data_field]));
//                return;
            if ($isEdit){
                $act = $this->model->find($data['id']);
            }
            if ($_FILES[$input_name]) {
                if ($act[$data_field]) {
                    //删除
                    $upConfig = C('UPLOADS');
                    removeFile($upConfig['PATH'] . '/' . $act[$data_field]);
                }
//                $this->error($data_field.$info[$input_name][$input_name]['savepath'] . $info[$input_name][$input_name]['savename']);
//                $this->error(json_encode($info));
//                return;
//                    $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                $data[$data_field] = $info[$input_name][$input_name]['savepath'] . $info[$input_name][$input_name]['savename'];
            }
            //上传资格证
            $input_name = 'certCover';
                if ($_FILES[$input_name]) {
                $data_field = 'certCover';
                if ($act[$data_field]) {
                    //删除
                    $upConfig = C('UPLOADS');
                    removeFile($upConfig['PATH'] . '/' . $act[$data_field]);
                }
//                    $info = $up->upFile($save_path, array('jpg', 'png', 'jpeg', 'gif'));
                $data[$input_name] = $info[$input_name][$input_name]['savepath'] . $info[$input_name][$input_name]['savename'];
            }
        }
        return $data;
    }

    /**
     * @param $id
     * 显示医生资格证审核页面
     */
    public function showReviewed($id){
        if (!$id) {
            $this->error('非法操作');
        }
        $select = $this->model->find($id);
        if (empty($select)) {
            $this->error('非法操作');
        }
        $this->assign('data', $select);
//        dump($select);
        $this->display();
    }

    /**
     * @param $id
     * 显示医生信息
     */
    public function showInfo($id){
        //查询助手医生信息
        $data = $this->model->getOne($id);
        $this->assign('data',$data);
        //查询用户信息
        $this->show('');
    }
    /**
     * 删除
     * @param string $id 要删除的id，多个以,分割
     */
    public function del($id)
    {
        $arr = explode(',', $id);
        $i = 0;
        foreach ($arr as $k => $v) {
            if (!$v) {
                continue;
            }
            $this->model->where('id=' . $v)->setField('status',-1);
            $i++;
        }
        $this->success('删除' . $i . '条数据', $this->urlConfig['del_success']);
    }

    /**
     * 审核页面的不通过给该医生发送短信
     */
    public  function editstatus(){
        $data=$this->model->find(I('post.id'));
        if(!I('post.id')){
            $this->error('非法操作');
         }
         if($data['status']==I('post.status')){
             $where['id']=I('post.id');
             $where['status']=I('post.status');
             $result=$this->model->save($where);
             if($result){
                 $this->success('修改成功!');
             }else{
                 $this->success('未修改任何数据!');
             }
          }else{
             if(I('post.status')==2){
                 $type=2;
                 $this->sendmsg($type,$data['phone']);
             }
             if(I('post.status')==1){
                 $type=1;
                 $this->sendmsg($type,$data['phone']);
             }
         }
         $where['id']=I('post.id');
         $where['status']=I('post.status');
         $result=$this->model->save($where);
         if($result && I('post.status')==2){
             $this->success('提交成功,并给该医生发出了信息提醒!');
         }else{
             $this->success('提交成功!');
         }
    }
    /**
     * 短信验证码
     */
    public  function sendmsg($type,$phone){
        $bmobSms = new  BmobSms();
        if($type==2){
            $mb="YZ_PHP";
        }else{
            $mb="PHP_DOC";
           }
        $res =$bmobSms->sendSmsVerifyCode("$phone", "$mb");  //发送短信验证码
        return $res;
    }

}